﻿using System;
using System.Linq.Functional;
using System.Windows;
using System.Windows.Controls;
using SoftSource.MVVMC;
using SoftSource.Reactive;
using SoftSource.Timecard.Resources;
using SoftSource.Timecard.ViewModelInterfaces;
using SoftSource.Timecard.Web.Data;

namespace SoftSource.Timecard.Views
{
    public sealed partial class ReportListView : UserControl, IView<IReportListViewModelConsume>
    {
        #region Fields
        private DataForm currentDataForm;

        private static Func<string, DataTemplate> GetDataTemplateforKeyMemoized;
        private static Func<string, DataTemplate> GetDataTemplateforKey = (string templateKey) =>
            {
                DataTemplate template = App.Current.Resources[templateKey] as DataTemplate;
                return template;
            };
        #endregion Fields

        public ReportListView()
        {
            InitializeComponent();

            ReportListView.GetDataTemplateforKeyMemoized = ReportListView.GetDataTemplateforKey.Memoize();
            Loaded += new RoutedEventHandler(ReportListView_Loaded);
        }

        #region Events

        void ReportListView_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.ViewModel != null)
            {
                this.Busy.BusyContent = ApplicationStrings.LoadingMessage;
                this.Busy.IsBusy = true;

                this.ViewModel.Load // Load the data required to use this ViewModel
                    .Nullable()
                    .Subscribe(unit => this.Busy.IsBusy = false);
            }
        }

        private void ReportListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Report report = listBox.SelectedItem as Report;

            if (report != null)
            {
                this.ReportParameterDataFormBorder.Child = null;

                if (report.TemplateKey != "None")
                {
                    this.currentDataForm = new DataForm();

                    DataTemplate template = GetDataTemplateforKeyMemoized(report.TemplateKey);
                    this.currentDataForm.EditTemplate = template;
                    this.currentDataForm.CurrentItem = report.GetDataObject();

                    this.ReportParameterDataFormBorder.Child = this.currentDataForm;
                }
            }
        }

        private void ViewReport_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Report report = listBox.SelectedItem as Report;

            if (this.currentDataForm == null)
            {
                report.OpenReport(null);
            }
            else if (this.currentDataForm.ValidateItem() && report != null)
            {
                report.OpenReport(this.currentDataForm.CurrentItem);
            }
        }

        #endregion Events

        #region IView<IReportListViewModelConsume> Members

        public IReportListViewModelConsume ViewModel
        {
            get { return this.DataContext as IReportListViewModelConsume; }
            set { this.DataContext = value; }
        }

        #endregion
    }
}
